package com.becca.cake.controller.system;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.model.system.FunctionDO;
import com.becca.cake.service.system.IUserFunctionService;
import com.becca.cake.util.JsonUtil;
import com.becca.cake.util.StringUtil;

/**
 * 功能管理
 * @author hejinguo
 * @version $Id: UserFunctionAction.java, v 0.1 2013-12-3 下午01:47:44
 */
@Controller
@RequestMapping("/systemFunction")
public class UserFunctionAction extends BaseAction {
    private final static Log     logger = LogFactory.getLog(UserLoadMenuAction.class);
    @Autowired
    private IUserFunctionService iUserFunctionService;

    /**
     * 功能管理中加载功能管理树
     * @author hejinguo
     * @return
     */
    @RequestMapping(value = "/selectFunctionTreeMenu", method = RequestMethod.POST)
    public @ResponseBody
    String selectFunctionTreeMenu(@RequestParam(value = "isUnable", required = false) String isUnable) {
        try {
            return this.iUserFunctionService.selectFunctionTreeMenu(isUnable);
        } catch (Exception e) {
            logger.error("功能管理中查询功能树失败!", e);
        }
        return null;
    }

    /**
     * 根据ID查询功能信息
     * @author hejinguo
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectFunctionDOById", method = RequestMethod.POST)
    public @ResponseBody
    String selectFunctionDOById(@RequestParam(value = "id", required = false) String id) {
        try {
            return this.iUserFunctionService.selectFunctionDOById(id);
        } catch (Exception e) {
            logger.error("功能管理中根据ID查询功能信息失败!", e);
        }
        return null;
    }

    /**
     * 添加功能信息
     * @author hejinguo
     * @param functionDO
     * @return
     */
    @RequestMapping(value = "/insetFunction", method = RequestMethod.POST)
    public @ResponseBody
    String insetFunction(@ModelAttribute FunctionDO functionDO) {
        if (functionDO != null) {
            functionDO.setCreatedMan(this.getUserSeesion().getTrueName());
            try {
                this.iUserFunctionService.insetFunction(functionDO);
                return JsonUtil.returnJsonSuccess("添加成功!");
            } catch (Exception e) {
                logger.error("添加失败!", e);
                return JsonUtil.returnJsonUnSuccess("添加失败!");
            }
        } else {
            logger.error("添加功能信息时信息为空!");
            return JsonUtil.returnJsonUnSuccess("添加功能信息时信息为空!");
        }
    }

    /**
     * 修改功能信息
     * @author hejinguo
     * @param functionDO
     * @return
     */
    @RequestMapping(value = "/updateFunctionById", method = RequestMethod.POST)
    public @ResponseBody
    String updateFunctionById(@ModelAttribute FunctionDO functionDO) {
        if (functionDO != null) {
            functionDO.setUpdatedMan(this.getUserSeesion().getTrueName());
            try {
                this.iUserFunctionService.updateFunctionById(functionDO);
                return JsonUtil.returnJsonSuccess("修改成功!");
            } catch (Exception e) {
                logger.error("修改失败!", e);
                return JsonUtil.returnJsonUnSuccess("修改失败!");
            }
        } else {
            logger.error("修改功能信息时信息为空!");
            return JsonUtil.returnJsonUnSuccess("修改功能信息时信息为空!");
        }
    }

    /**
     * 根据角色ID查询拥有的功能
     * @author hejinguo
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/selectOwnFunctionByRoleId", method = RequestMethod.POST)
    public @ResponseBody
    String selectOwnFunctionByRoleId(@RequestParam(value = "roleId", required = false) String roleId) {
        if (StringUtil.isNotEmpty(roleId)) {
            try {
                return JsonUtil.returnJsonSuccess(this.iUserFunctionService
                    .selectOwnFunctionByRoleId(roleId));
            } catch (Exception e) {
                logger.error("根据角色ID查询拥有的功能时查询失败!", e);
                return JsonUtil.returnJsonUnSuccess("根据角色ID查询拥有的功能时查询失败!");
            }
        } else {
            logger.error("根据角色ID查询拥有的功能时角色ID为空!");
            return JsonUtil.returnJsonUnSuccess("根据角色ID查询拥有的功能时角色ID为空!");
        }
    }

    /**
     * 给角色授权功能
     * @author hejinguo
     * @param roleId  角色ID
     * @param rights  功能ID
     * @return
     */
    @RequestMapping(value = "/updateGrantRoleFunction", method = RequestMethod.POST)
    public @ResponseBody
    String updateGrantRoleFunction(@RequestParam(value = "roleId", required = false) String roleId,
                                   @RequestParam(value = "rights", required = false) String rights) {
        if (StringUtil.isNotEmpty(roleId) && StringUtil.isNotEmpty(rights)) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("roleId", roleId);
            map.put("rights", rights.split("[,]"));
            map.put("createMan", this.getUserSeesion().getTrueName());
            try {
                this.iUserFunctionService.updateGrantRoleFunction(map);
                return JsonUtil.returnJsonSuccess("授权成功!");
            } catch (Exception e) {
                logger.error("授权失败!", e);
                return JsonUtil.returnJsonUnSuccess("授权失败!");
            }
        } else {
            logger.error("给角色授权功能时角色ID为空或功能ID为空!");
            return JsonUtil.returnJsonUnSuccess("给角色授权功能时角色ID为空或功能ID为空!");
        }
    }

    /**
     * 根据员工ID查询功能信息
     * @author hejinguo
     * @param adminId
     * @return
     */
    @RequestMapping(value = "/selectFunctionByAdminId", method = RequestMethod.POST)
    public @ResponseBody
    String selectFunctionByAdminId(@RequestParam(value = "adminId", required = false) String adminId) {
        if (StringUtil.isNotEmpty(adminId)) {
            try {
                return this.iUserFunctionService.selectFunctionByAdminId(adminId);
            } catch (Exception e) {
                logger.error("根据员工ID查询功能信息失败!");
                return JsonUtil.returnJsonUnSuccess("根据员工ID查询功能信息失败!");
            }
        } else {
            logger.error("根据员工ID查询功能信息时员工ID为空!");
            return JsonUtil.returnJsonUnSuccess("根据员工ID查询功能信息时员工ID为空!");
        }
    }
}
